package org.alerve.android.resourcebuilder.fieldmodules;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.alerve.android.resourcebuilder.Resource.ArrayType;

import android.content.Context;
import android.content.res.Resources;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * 
 * Annotation used to attach a
 * 
 * @author pabs87@gmail.com
 * 
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SpinnerArrayAdapter {

	public static final int SPINNER_DROP_DOWN = android.R.layout.simple_spinner_dropdown_item;
	public static final int SPINNER_ITEM_DEFAULT = android.R.layout.simple_spinner_item;

	/**
	 * Id for the array to be fetched from the resources
	 */
	public int value();

	/**
	 * Describes the type of array that will be loaded into the adapter.
	 */
	public ArrayType type() default ArrayType.String;

	/**
	 * 
	 * Describes the layout to be used with the {@link Spinner} along with the
	 * id of the field to fill in. It is used in
	 * {@link SpinnerArrayAdapter#layout()}
	 * 
	 * @author pabs87@gmail.com
	 * 
	 */
	public @interface SpinnerLayout {
		public int value();
		public int textViewResourceId() default 0;
	}

	/**
	 * Uses a {@link SpinnerLayout} to define the {@link Spinner}'s layout.
	 */
	public SpinnerLayout layout() default @SpinnerLayout(SPINNER_ITEM_DEFAULT);

	public int dropDownLayout() default SPINNER_DROP_DOWN;

	/**
	 * 
	 * Creator class used to instantiate {@link ArrayAdapter} with the provided
	 * {@link SpinnerArrayAdapter} annotation.
	 * 
	 * @author pabs87@gmail.com
	 * 
	 */
	public static class Creator {

		public static <T> ArrayAdapter<T> create(Context context, Field field) {
			return create(context,
					field.getAnnotation(SpinnerArrayAdapter.class));
		}

		@SuppressWarnings("unchecked")
		public static <T> ArrayAdapter<T> create(Context context,
				SpinnerArrayAdapter annotation) {
			if (annotation == null) {
				throw new NullPointerException("An annotation is required.");
			}
			int resource = annotation.layout().value();
			int textViewResourceId = annotation.layout().textViewResourceId();
			int spinnerDropDownId = annotation.dropDownLayout();
			List<T> objects = null;
			int objectsId = annotation.value();
			Resources resources = context.getResources();
			switch (annotation.type()) {
				case String :
					String[] strings = resources.getStringArray(objectsId);
					objects = (List<T>) Arrays.asList(strings);
					break;
				case Text :
					CharSequence[] sequences = resources
							.getTextArray(objectsId);
					objects = (List<T>) Arrays.asList(sequences);
				case Integer :
					int[] intObjects = resources.getIntArray(objectsId);
					List<Integer> intList = new ArrayList<Integer>(
							intObjects.length);
					for (int value : intObjects) {
						intList.add(value);
					}
					objects = (List<T>) Arrays.asList();
				default :
					break;
			}
			ArrayAdapter<T> adapter = new ArrayAdapter<T>(context, resource,
					textViewResourceId, objects);
			adapter.setDropDownViewResource(spinnerDropDownId);
			return (ArrayAdapter<T>) adapter;
		}
	}

}
